package ${package.Controller};

import org.springframework.web.bind.annotation.RequestMapping;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.core.domain.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.CodeUtils;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Api(tags  = "${table.comment!}")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
@RequiredArgsConstructor
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    private static final String CODE_KEY = "${entity?uncap_first}:";
    private final ${table.serviceName} ${table.serviceName?uncap_first};
    private final RedisCache redisCache;

    @ApiOperation(value = "${table.comment!}列表", response = ${entity}.class)
    @GetMapping("/list")
    public TableDataInfo list(${entity} ${entity?uncap_first}){
        startPage();
        List<${entity}> list = ${table.serviceName?uncap_first}.selectList(${entity?uncap_first});
        return getDataTable(list);
    }

    @ApiOperation(value = "新增${table.comment!}")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ${entity} ${entity?uncap_first}){
        ${table.serviceName?uncap_first}.save(${entity?uncap_first});
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改${table.comment!}")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ${entity} ${entity?uncap_first}){
        ${table.serviceName?uncap_first}.updateById(${entity?uncap_first});
        return AjaxResult.success();
    }

    @ApiOperation(value = "根据id查询单条${table.comment!}")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id){
            LambdaQueryWrapper<${entity}> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(id!= null,${entity}::getId, id);
            ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.getOne(wrapper);
            return AjaxResult.success(${entity?uncap_first});
    }

    @ApiOperation(value = "删除${table.comment!}")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        return AjaxResult.success(${table.serviceName?uncap_first}.removeById(id));
    }

    @ApiOperation(value = "批量删除${table.comment!}")
    @DeleteMapping("/batchRemove/{ids}")
    public AjaxResult batchRemove(@PathVariable Long[] ids){
        return AjaxResult.success(${table.serviceName?uncap_first}.removeByIds(Arrays.asList(ids)));
    }


    @PostMapping("/export")
    @ApiOperation("导出${table.comment!}数据")
    public void export(HttpServletResponse response, ${entity} ${entity?uncap_first}){
    List<${entity}> list = ${table.serviceName?uncap_first}.selectList(${entity?uncap_first});
            ExcelUtil<${entity}> util = new ExcelUtil<>(${entity}.class);
                    util.exportExcel(response, list, "${table.comment!}数据");
    }

    @ApiOperation(value = "生成唯一编号")
    @GetMapping("/generateCode")
    public AjaxResult generateCode() {
        String code = ${table.serviceName?uncap_first}.generateCode();
        return AjaxResult.success("",code);
    }


    }
</#if>
